package com.shuzi.MediaShare.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shuzi.MediaShare.R;

/**
 * 自定义listview 仿gridview功能可添加headerView 和 footerView
 * 
 * @author lung
 * 
 * @param <T>
 */
public abstract class BucketListAdapter<T> extends BaseAdapter
{

	protected List<T> elements;

	protected Activity ctx;

	/** 列数 */
	protected int columns = 3;

	protected ViewHolderGroup holderGroup = null;

	/** 行数 */
	protected int rows = 0;

	/** 横向的修正间隔 */
	protected int space = 10;

	public int getColumns()
	{
		return columns;
	}

	public void setColumns(int columns)
	{
		this.columns = columns;
	}

	protected List<T> getElements()
	{
		return elements;
	}

	protected void setElements(List<T> elements)
	{
		this.elements = elements;
		rows = getCount();
	}

	public BucketListAdapter(Activity ctx, List<T> elements)
	{
		// this(ctx, elements, 1);
		this.elements = elements;
		this.ctx = ctx;
		rows = getCount();
	}

	@Override
	public int getCount()
	{
		if (elements.size() == 0)
		{
			return 0;
		}
		if (this.elements.size() <= columns)
		{
			return 1;
		} else
		{
			if (elements.size() % columns == 0)
			{
				return elements.size() / columns;
			}
			return elements.size() / columns + 1;
		}
	}

	@Override
	public Object getItem(int position)
	{
		return null;
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	public class ViewHolder
	{
		public View baseView;

		public LinearLayout layout;

		/** 此item的索引位置 */
		public int index = 0;

		/** 参数 */
		public LinearLayout.LayoutParams params = null;

		public ViewHolder(LinearLayout layout, int index)
		{
			this.layout = layout;
			this.index = index;
			// setItemLayoutParams();
			layout.addView(baseView);

		}

		/**
		 * 设置内容
		 * 
		 * @param position
		 * @param currentElement
		 */
		public void setInfo(int position, T currentElement)
		{
			setItemInfo(position, currentElement, baseView);
		}

		// /**
		// * 修正单数显示时效果
		// */
		// public void setItemLayoutParams()
		// {
		// if (columns % 2 != 0 && columns > 1)
		// {
		// int centerIndex = (columns - 1) / 2;
		// params = new LinearLayout.LayoutParams(getScreen().widthPixels
		// / columns, (int) ctx.getResources().getDimension(
		// R.dimen.dip166));
		// if (index < centerIndex)
		// {
		// setItemLayoutParams(baseView, Gravity.RIGHT);
		// } else if (index == centerIndex)
		// {
		// setItemLayoutParams(baseView, Gravity.CENTER_HORIZONTAL);
		// } else
		// {
		// setItemLayoutParams(baseView, Gravity.AXIS_X_SHIFT);
		// }
		//
		// baseView.setLayoutParams(params);
		// }
		// }

		/**
		 * 控件居中
		 * 
		 * @param baseView
		 * @param gravity
		 */
		public void setItemLayoutParams(View baseView, int gravity)
		{
			if (baseView instanceof RelativeLayout)
			{
				RelativeLayout aLayout = (RelativeLayout) baseView;
				aLayout.setGravity(gravity);
			} else if (baseView instanceof LinearLayout)
			{
				LinearLayout aLayout = (LinearLayout) baseView;
				aLayout.setGravity(gravity);
			}
		}

		/**
		 * 设置是否显示
		 * 
		 * @param isVisibility
		 */
		public void setVisibility(boolean isVisibility)
		{
			if (isVisibility)
			{
				baseView.setVisibility(View.VISIBLE);
			} else
			{
				baseView.setVisibility(View.GONE);
			}
		}

	}

	public final class ViewHolderGroup
	{
		public List<ViewHolder> list;

		public View baseView;

		public LinearLayout layout;

		public ViewHolderGroup(View baseView)
		{
			this.baseView = baseView;
			layout = (LinearLayout) baseView
					.findViewById(R.id.horizontalLayout);
		}

		/**
		 * 得到每一行的items
		 * 
		 * @return
		 */
		public List<ViewHolder> getList()
		{
			list = new ArrayList<BucketListAdapter<T>.ViewHolder>();
			for (int i = 0; i < columns; i++)
			{
				ViewHolder holder = new ViewHolder(layout, i);
				list.add(holder);
			}
			return list;
		}

		/**
		 * 设置最后一行的数据
		 * 
		 * @param position
		 */
		public void setLastRowInfo(int position)
		{
			int count = elements.size() % columns;
			int nowPosition;
			int goneFirstPosition = 0;
			for (int i = 0; i < count; i++)
			{
				ViewHolder holder = list.get(i);
				nowPosition = columns * position + i;
				holder.setInfo(nowPosition, elements.get(nowPosition));
				goneFirstPosition = i + 1;
			}
			int columnsCount = columns;
			if (count != 0)
			{
				for (int i = 0; i < columns; i++)
				{
					list.get(i).setVisibility(true);
				}
				for (int j = goneFirstPosition; j < columnsCount; j++)
				{
					// GoLogUtil.e("j ="+j+"setVisibility");
					list.get(j).setVisibility(false);
				}
			}
			return;
		}

		public void setList(List<ViewHolder> list)
		{
			this.list = list;
		}

		/**
		 * 填充数据
		 * 
		 * @param position
		 */
		public void setInfo(int position)
		{
			// Log.e("lung", "position = " + position + "columns =" + columns);
			int nowPosition = 0;
			if (list != null && list.size() != 0)
			{
				int count = 0;
				for (ViewHolder holder : list)
				{
					if (getColumns() == 1)
					{
						holder.setInfo(position, elements.get(position));
					} else
					{
						nowPosition = columns * position + count;
						holder.setInfo(nowPosition, elements.get(nowPosition));
						list.get(count).setVisibility(true);
					}
					count++;
				}
			}
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		rows = getCount();
		if (convertView == null)
		{
			convertView = View.inflate(ctx, R.layout.bucket, null);
			holderGroup = new ViewHolderGroup(convertView);
			holderGroup.list = holderGroup.getList();
			convertView.setTag(holderGroup);
		} else
		{
			holderGroup = (ViewHolderGroup) convertView.getTag();
		}

		if (elements.size() % columns == 0)
		{
			holderGroup.setInfo(position);
		} else
		{
			// 如果不是最后一行的话
			if (position != rows - 1)
			{
				holderGroup.setInfo(position);
			} else
			{
				holderGroup.setLastRowInfo(position);
			}
		}
		return convertView;
	}

	/**
	 * 设置数据
	 * 
	 * @param position
	 * @param currentElement
	 * @param baseView
	 */
	protected abstract void setItemInfo(final int position, T currentElement,
			View baseView);

	/**
	 * 返回每一个item项
	 * 
	 * @return
	 */
	protected abstract View getItemView();

}
